package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }


    /*
    * 根据id删除部门，
    * 同时应该删除对应部门下的员工
    * 这里要开启事务管理*/
    @Override
    @Transactional
    public void deleteById(Integer id) {
//        根据id删除部门
        deptMapper.deleteById(id);
//        删除对应部门下的员工
        empMapper.deleteByDeptId(id);
    }

    @Override
    public void save(Dept dept) {
        /*
         *添加时间属性
         */
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.save(dept);
    }

    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    @Override
    public void update(Dept dept) {
        /*
         *添加时间属性
         */
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
